package com.eliza.base.views.learn.layouts.material

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectDragGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.DrawerValue
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExtendedFloatingActionButton
import androidx.compose.material3.FloatingActionButton
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalDrawerSheet
import androidx.compose.material3.ModalNavigationDrawer
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.material3.rememberDrawerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import kotlin.math.roundToInt

@Preview
@Composable
private fun LayoutMaterialScreenPre() {
    LayoutMaterialX.Show(rememberNavController())
}

object LayoutMaterialX {
    @Composable
    fun Show(navController: NavController) {
        Column {
            var count by remember {
                mutableIntStateOf(0)
            }
            Button(onClick = { count = (count + 1) % 4 }) {
                Text(text = "點擊切換")
            }
            when (count) {
                0 -> simple()
                1 -> btn()
                2 -> modalNavigationDrawer()
                3 -> bottomDrawer()
            }
        }
    }


    @Composable
    private fun simple() {
        Box(modifier = Modifier.fillMaxSize()) {
            var offsetX by remember { mutableFloatStateOf(0f) }
            var offsetY by remember { mutableFloatStateOf(0f) }
            Box(
                Modifier
                    .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
                    .background(Color.Gray)
                    .width(100.dp)
                    .height(50.dp)
                    .pointerInput(Unit) {
                        detectDragGestures { change, dragAmount ->
                            change.consume()
                            offsetX += dragAmount.x
                            offsetY += dragAmount.y
                        }
                    }
            ) {
                ExtendedFloatingActionButton(onClick = {
                }) {
                    Text("floatingActionButton")
                }
                FloatingActionButton(onClick = { /* ... */ }) {
                    Text("a")
                }
            }
        }

    }

    //悬浮操作按钮点按操作
    @Composable
    private fun btn() {
        val scope = rememberCoroutineScope()
        Scaffold(floatingActionButton = { simple() }, bottomBar = {
            BottomAppBar(
                modifier = Modifier
                    .background(Color.Black)
                    .height(50.dp),
                containerColor = Color.Gray
            ) {
                Text("this BottomAppBar")
            }
        }) { it ->
            Column(
                Modifier.padding(it)
            ) {
                Button(
                    onClick = { /* ... */ }, contentPadding = PaddingValues(
                        start = 20.dp, top = 12.dp, end = 20.dp, bottom = 12.dp
                    )
                ) {
                    Icon(
                        Icons.Filled.Favorite,
                        contentDescription = "Favorite",
                        modifier = Modifier.size(ButtonDefaults.IconSize)
                    )
                    Spacer(Modifier.size(ButtonDefaults.IconSpacing))
                    Text("Like")
                }

                ExtendedFloatingActionButton(onClick = { }, icon = {
                    Icon(
                        Icons.Filled.Favorite, contentDescription = "Favorite"
                    )
                }, text = { Text("ExtendedFloatingActionButton") })
                Text(text = "$it.toString")
                modalNavigationDrawer()
            }

        }
    }

//抽屉式导航栏

    @Composable
    private fun modalNavigationDrawer() {
        val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
        ModalNavigationDrawer(drawerState = drawerState, drawerContent = {
            Box(modifier = Modifier.background(Color.Yellow)) {
                Column {
                    Text(text = "drawerContent")
                    bottomDrawer()
                    bottomSheetScaffold()
                }
            }

        }) {
            Column(
                modifier = Modifier
                    .fillMaxSize(0.5F)
                    .background(Color.Cyan)
            ) {
                repeat(4) {
                    Text(text = "ModalNavigationDrawer 左划")
                }
            }
        }
    }

    @Composable
    private fun bottomDrawer() {
        ModalDrawerSheet(
            modifier = Modifier
                .height(100.dp)
                .background(Color.Yellow)
        ) {
            repeat(10) {
                Text(
                    text = "ModalDrawerSheet",
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.background(Color.White)
                )
            }
        }
    }

    //底部动作条
    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    private fun bottomSheetScaffold() {
        val scaffoldState = rememberBottomSheetScaffoldState()
        BottomSheetScaffold(
            scaffoldState = scaffoldState,
            sheetContent = {
                repeat(6) {
                    Text(text = "bottomSheetScaffold")
                }
            },
        ) {
            Text(text = "BottomSheetScaffold $it")
        }
    }

}